#include <linux/version.h>
#include <linux/input.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

#include "TouchPanel.h"
#include "DrawTool.h"
#include "IrControl.h"

#define     INPUT_DEVICE_NAME           "/dev/input/event0"

#define     MAX_EVENT                   16

int TS_KX = 100;
int TS_KY = 100;
int TP_CLBR_C_LX = TP_CLBR_C_X;//logic x
int TP_CLBR_C_LY = TP_CLBR_C_Y;//logic y

static int  _OldPositionX = 0;
static int  _OldPositionY = 0;

static int  _PositionX = 0;
static int  _PositionY = 0;

static int  _KeyStatus = IRC_NO_KEY;

static int  _OffsetX = 0;	
static int  _OffsetY = 0;
//Bill modify
int  tsdev_fd;
extern unsigned char navi_status;
extern unsigned char read_info_data[20];
extern unsigned char KeyData;

int  dev_TouchPanelInit(void)
{
    if((tsdev_fd = open(INPUT_DEVICE_NAME,O_RDONLY)) < 0){
      perror("evtest");

      //Bill test
      //exit(1);
    }

    return 0;
}


#ifdef  CD_TEST_MODE
int dev_TouchPanelRead(void)
{
    int     Index;
 
    int     Result;

    struct input_event event[MAX_EVENT];

    //Bill test
    if(tsdev_fd < 0 || navi_status)
      return IRC_NO_KEY;
      
//    if (!TPEnableFlag)
	//	 return IRC_NO_KEY;///////////20080416


    Result = read(tsdev_fd,event, sizeof(struct input_event) * MAX_EVENT);    

    if(Result < (int)sizeof(struct input_event)){
      perror("Read input device error\n");

      return IRC_NO_KEY;
    }

     _PositionX = _OldPositionX;
     _PositionY = _OldPositionY;

    for(Index = 0; Index < Result / sizeof(struct input_event);Index++){
    	//printf("type %d\n",event[Index].type);    //20071214	
            	
      switch(event[Index].type){
      case 0:
        switch(_KeyStatus){
        case IRC_TOUCH_UP:
          _KeyStatus = IRC_TOUCH_DOWN;

          break;
        case IRC_TOUCH_DOWN:
          _KeyStatus = IRC_TOUCH_DOWN;

          break;
/*
        case IRC_TOUCH_DOWN:
        case IRC_TOUCH_REPEAT:        	
          _KeyStatus = IRC_TOUCH_REPEAT;
          break;            
*/         
        default: 
         ;// _KeyStatus = IRC_TOUCH_DOWN;   //////////////hangsheng masked for first touch not active.        	  
        }

        break;

      case 1:
      	if(event[Index + 1].type == 0){
          _KeyStatus = IRC_TOUCH_UP;    
        }
        else{
          _KeyStatus = IRC_TOUCH_DOWN;        	
        }

        break;

      case 3:
        switch(event[Index].code){
        case 0:
          _PositionX = event[Index].value;

          break;

        case 1:
          _PositionY = event[Index].value;

          break;
          }

        break;
      }      
      
      if(_KeyStatus == IRC_TOUCH_UP){
      	break;
      }
    }
    
    _OldPositionX = _PositionX;
    _OldPositionY = _PositionY;      
    
    // printf("%d\n",_KeyStatus);

    return _KeyStatus;
}

void dev_TouchPanelGetXY(int* x,int* y)
{
    
    *x = 720 - (_PositionX * 720) / 4096;
    *y = 480 - (_PositionY * 480) / 4096;
    //printf ("TS_KX Iis %d,TS_KY is %d,x is %d,y is %d \n",TS_KX,TS_KY,*x,*y);
    if ( (TS_KX != 100) && (TS_KX > TP_KXY_LI) && (TS_KX < TP_KXY_HI))
    	{
    		*x = (100*(*x - TP_CLBR_C_LX))/TS_KX +TP_CLBR_C_X;
    	}
    if ( (TS_KY != 100) && (TS_KY > TP_KXY_LI) && (TS_KY< TP_KXY_HI))
    	{
    		*y = (100*(*y - TP_CLBR_C_LY))/TS_KY +TP_CLBR_C_Y;    		
    	} 
    printf("X %d Y %d\n",*x,*y);
}

#endif




#ifdef MODULE_TEST_MODE
int dev_TouchPanelRead(void)
{

	if (dev_UartRead(read_info_data))
		{
  		return IRC_NO_KEY;
		}
	else
		{			
			if ((read_info_data[0] == MODULE_TEST_FLAG1) && (read_info_data[1] == MODULE_TEST_FLAG2))
			{	
			
				switch ( read_info_data[2])
				{
					case 	MODULE_TEST_INPUT_MOUSE:
					switch 	(read_info_data[5])
					{	
						case  MODULE_TEST_PRESS_DOWN:				
					 		_KeyStatus = IRC_TOUCH_DOWN;
							_PositionX =  read_info_data[3] ;
							_PositionY=  read_info_data[4] ;
							break;
						case  MODULE_TEST_PRESS_UP:				
						 	_KeyStatus = IRC_TOUCH_UP;
							_PositionX =  read_info_data[3] ;
							_PositionY = read_info_data[4] ;
							break;
						default:
							{
								printf ("data wrong, NO KEY handle.\n");
								return IRC_NO_KEY;
							}
					}		
						break;
						
					case	MODULE_TEST_INPUT_KEYBOARD:
						if (read_info_data[3] != MODULE_TEST_KEY_UP)
							KeyData = read_info_data[3];
					//	if (ir_GetCurrentID() ==IR_TEST_VERSION)
							DispVerInit();
						break;
			
				}
						
				
			}
				else
					{
						memset (read_info_data,0,sizeof(read_info_data));
						return IRC_NO_KEY;						
					}
			memset (read_info_data,0,sizeof(read_info_data));
		}
	return _KeyStatus;
}


void dev_TouchPanelGetXY(int* x,int* y)
{
    
    //*x = 720 - (_PositionX * 720) / 4096;
    //*y = 480 - (_PositionY * 480) / 4096;
    *x = (_PositionX*18)/5;
    *y =( _PositionY*30)/7;
   
    printf("X %d Y %d\n",*x,*y);
}

#endif




void dev_TouchPanelSetOffset(int offsetX, int offsetY)
{

}

void dev_AudioJackInit()
{
}

void dev_AudioJackDetect()
{
}
